package com.xique.system.controller;

import com.xique.common.core.domain.R;
import com.xique.common.core.utils.AliossUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.system.api.dto.SystemProjectDTO;
import com.xique.system.api.request.RemoteSystemProjectAllListRequest;
import com.xique.system.bean.request.*;
import com.xique.system.service.biz.service.ISystemProjectBizService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @author shd
 * @create 2021/12/9 16:49
 */
@RestController
@RequestMapping("/project")
public class SystemProjectController extends BaseController {

    @Autowired
    private ISystemProjectBizService projectBizService;

    @Autowired
    private AliossUtil aliossUtil;

    @GetMapping("/info/{parkId}")
    @ApiOperation("登录时获取当前用户信息")
    public R<SystemProjectDTO> info(@PathVariable("parkId") Long parkId) {
        return projectBizService.getProjectInfo(parkId);
    }

    @PreAuthorize(hasPermi = "system:project:list")
    @GetMapping("/list")
    public AjaxResult list(SystemProjectListRequest request) {
        return projectBizService.getProjectList(request);
    }

    @PreAuthorize(hasPermi = "system:project:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody SystemProjectAddRequest request) {
        return projectBizService.addProject(request);
    }

    @PreAuthorize(hasPermi = "system:project:edit")
    @GetMapping("/detail")
    public AjaxResult detail(Long id) {
        return projectBizService.getProjectDetail(id);
    }

    @PreAuthorize(hasPermi = "system:project:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Valid @RequestBody SystemProjectEditRequest request) {
        return projectBizService.editProject(request);
    }

    @PreAuthorize(hasPermi = "system:project:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("remove")
    public AjaxResult remove(String id) {
        List<String> idList = Arrays.asList(id.split(","));
        for (String projectId : idList) {
            AjaxResult result = projectBizService.delProject(Long.valueOf(projectId));
            if (AjaxResult.isError(result)) {
                return AjaxResult.error();
            }
        }
        return AjaxResult.success();
    }

    @GetMapping("/v1/list")
    public AjaxResult listV1(SystemProjectListRequest request) {
        return projectBizService.getProjectList(request);
    }

    @PostMapping("/allList")
    public R<List<SystemProjectDTO>> allList(@RequestBody RemoteSystemProjectAllListRequest request) {
        return projectBizService.getAllProjectList(request);
    }

    @PreAuthorize(hasPermi = "system:project:edit")
    @GetMapping("/icbcConfigDetail")
    public AjaxResult icbcConfigDetail(Long parkId) {
        return projectBizService.getIcbcConfigDetail(parkId);
    }

    @PreAuthorize(hasPermi = "system:project:add")
    @PostMapping("/addParkPayIcbc")
    public AjaxResult addParkPayIcbc(@Valid @RequestBody IcbcAddVendorRequest request) {
        return projectBizService.addParkPayIcbc(request);
    }

    @PreAuthorize(hasPermi = "system:project:edit")
    @PutMapping("/updateParkPayIcbc")
    public AjaxResult updateParkPayIcbc(@Valid @RequestBody IcbcUpdateVendorRequest request) {
        return projectBizService.updateParkPayIcbc(request);
    }

    @PreAuthorize(hasPermi = "system:project:edit")
    @PostMapping("/uploadPic")
    public AjaxResult uploadPic(@RequestBody ProjectUploadPicRequest request) {
        return projectBizService.uploadPic(request);
    }
}

